/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */


layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;

  form.render(null, 'layadmin-departmentfront-formlist');
  //监听搜索
  form.on('submit(LAY-department-front-search)', function(data){
    var field = data.field;

    //执行重载
    table.reload('LAY-department-manage', {
      page: {
        curr: 1
      }
      ,where: field
      ,method:'post'
    });
  });

  //部门数据管理
  table.render({
    elem: '#LAY-department-manage'
    ,url: '/getAllDepartmentsPageServlet' //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'departmentId', width: 100, title: 'ID', sort: true,hide:true}
      ,{field: 'departmentName', title: '部门名称', minWidth: 100,sort: true}
      ,{field: 'pdname', title: '上级部门', minWidth: 100,sort: true}
      ,{field: 'departmentPid', title: '父编号', minWidth: 100,sort: true,hide:true}
      ,{field: 'departmentFlag', title: '状态'}
      ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#table-departmentadmin-webdepartment'}
    ]]
    ,page: true
    ,limit: 15
    ,height: 'full-320'
    ,text: '对不起，加载出现异常！'
  });

  //监听工具条
  table.on('tool(LAY-department-manage)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){//删除
      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);

        layer.confirm('真的删除行么', function(index){
          $.ajax({
              type: 'POST'
              ,url: "/deleteDepartmentServlet"
              ,data: {departmentId:data.departmentId}
              ,success: function(data){
                  if(data==="Ok"){
                    layer.alert('删除成功', {icon: 6});
                  }else{
                    layer.alert(data,{title:'删除失败'});
                  }
              }
          })
          obj.del();
          layer.close(index);
        });
      });
    } else if(obj.event === 'edit'){//编辑
      admin.popup({
        title: '编辑用户'
        ,area: ['500px', '350px']
        ,id: 'LAY-popup-department-add'
        ,success: function(layero, index){
          view(this.id).render('department/update', data).done(function(){
            form.render(null, 'layuiadmin-form-departmentadmin');

            //监听提交
            form.on('submit(LAY-department-front-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              $.ajax({
                type: 'POST'
                ,url: "/updateDepartmentServlet"
                ,data: field
                ,success: function(data){
                    if(data==="Ok"){
                      layer.alert('修改成功', {icon: 6});
                    }else{
                      layer.alert(data,{title:'修改失败'});
                    }
                }
              });
              layui.table.reload('LAY-department-manage'); //重载表格
              layer.close(index); //执行关闭
            });
          });
        }
      });
    }
  });





  exports('departmentadmin', {});

  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-department-manage')
          ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }

      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);

        layer.confirm('确定删除吗？', function(index) {

          //执行 Ajax 后重载
          /*
          admin.req({
            url: 'xxx'
            //,……
          });
          */
          table.reload('LAY-department-manage');
          layer.msg('已删除');
        });
      });
    }
    ,add: function(){
      admin.popup({
        title: '添加部门'
        ,area: ['500px', '300px']
        ,id: 'LAY-popup-department-add'
        ,success: function(layero, index){
          view(this.id).render('department/add').done(function(){
            form.render(null, 'layuiadmin-form-departmentadmin');

            //监听提交
            form.on('submit(LAY-department-front-submit)', function(data){//此处的data为前端页面字段
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              $.ajax({
                type:'POST'
                ,url: '/addDepartmentServlet'
                ,data: field
                ,success: function(data){//此处的data为后端返回的数据，即response
                  if(data==="Ok"){
                    layer.alert("添加成功！",{icon: 6});
                  }else{
                    layer.alert(data,{title: "添加失败!"});
                  }
                }
              });
              layui.table.reload('LAY-department-manage'); //重载表格
              layer.close(index); //执行关闭
            });
          });
        }
      });
    }
  };

  $('.layui-btn.layuiadmin-btn-departmentadmin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

});